/*****************************************************************************
 *  CommLayerLib - DataLogger.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: DataLogger.java 32 2012-10-09 18:20:32Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer;


import java.io.*;

/**
 * Abstract class to implements objects that log data read by a {@link DataReader}.
 * DataLogger provides a separate thread to actually do the write job, so it will 
 * not block DataReader operations.
 * @author Matteo Piscitelli
 * @see DataReader
 */
public abstract class DataLogger extends DataWriter {
	protected String logURL;
	
	public final static String THREAD_NAME = "DataLogger";
	
	public DataLogger() {
		super();
		logURL = "";
	}

	public DataLogger(String logURL) {
		super();
		this.logURL = logURL;
	}
	
	/**
	 * Abstract method to be implemented. Successors of this class should implement
	 * this method to open an {@link OutputStream} and returning it for the DataLogger to 
	 * write to.
	 * @return
	 */
	protected abstract OutputStream openStream();
	
	
	public boolean openLog() {
		s = openStream();
		if (s == null) return false;

		start();
		return true;
	}

	public void closeLog() {
		//if (! isRunning()) return;
		
		terminate();
		if (s != null)
			try {
				s.close();
			} catch (IOException e) {
				// do nothing
			}
	}

	public void setLogURL(String url) {
		this.logURL = url;
	}

		
}
